package com.zxlspace.collection2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MyArrayList4 implements Iterable{
    private String[] array = new String[]{"a","b","c","d","e","f"};

    /*class MyIt implements Iterator {

    }*/

    public Iterator<String> iterator() {
        return  new Iterator<String>() {  //实现匿名内部类
            private int size = array.length;

            private int cusor = -1;  //游标位置

            //是否还有下一个元素
            public boolean hasNext() {
                return cusor+1 < size;
            }

            //返回下一个元素,游标往下移动一个位置
            public String next() {
                cusor++;
                return array[cusor];
            }

            public void remove() {
                System.arraycopy(array, cusor+1, array, cusor, size-(cusor+1));
                this.size--;
                //游标回退
                this.cusor--;
            }
        };
    }

    public static void main(String[] args) {
        MyArrayList4 myArrayList3 = new MyArrayList4();

        Iterator iterator = myArrayList3.iterator();
        int i=0;
       /* while (iterator.hasNext()) {
            System.out.println(iterator.next());
            iterator.remove();
            if (i==3) {
                System.out.println("12321");
            }
            i++;
        }*/

        for (Iterator iterator1 = myArrayList3.iterator(); iterator1.hasNext();) {
            System.out.println(iterator1.next());
        }

       /* Iterator iterator2 = myArrayList3.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }*/

        List<String> list = new ArrayList<>();
    }
}
